package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 网贷之家
 * @author Persh
 *
 */
public class WangdaizhijiaNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://news.wangdaizhijia.com/[0-9]{8}/[0-9]*.html",
		"http://www.wangdaizhijia.com/[a-z]*/\\?[a-z]*/[a-z]*/[0-9]*.html",
		"http://www.wangdaizhijia.com/wenda/index.php\\?question/view/[0-9]*.html",
		"http://www.wangdaizhijia.com/dangan/[a-z0-9]*/dianping/[0-9]*.html",
		"http://www.wangdaizhijia.com/[a-z]*/[a-z]*/[0-9]*.html",
		};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
			return null;
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div.commentFont p.font").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select("h2.atl_title").text();
		}
		
		content=HtmlCleaner.getContentHtml(url,doc.select("li.news_con_p"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.description"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.commentFont p.font"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div.brief"));
		}
		String authorStr=null;
		authorStr=doc.select("span#lbl_origin").text();
		author=JSoupUtils.matchAuthor(doc, "来源：");
		if(StringUtils.isBlank(author)) {
			authorStr=doc.select("div.user-label-info").text();
			author=StringUtils.substringBefore(authorStr, "|浏览");
		}
		if(StringUtils.isBlank(author)) {
			authorStr=doc.select("span.name").text();
			author=StringUtils.substring(authorStr, 0 );
		}
		if(StringUtils.isBlank(author)) {
			authorStr=doc.select("div.info-left").text();
			author=StringUtils.substringAfter(authorStr, "来源：" );
		}
		
		date=DateUtils.matchDate(doc.select("li.n_time").text());
		if(date == null) {
			date=DateUtils.matchDate(doc.select("span.span-line").text());
		}
		if(date == null) {
			date=DateUtils.matchDate(doc.select("span.date").text());
		}
		if(date == null) {
			date=DateUtils.matchDate(doc.select("div.info-left").text());
		}

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);	
		
		return news;
	}
	
	public static void main(String[] args) {
		WangdaizhijiaNewsAnalyse analyse=new WangdaizhijiaNewsAnalyse();
		String url="http://www.wangdaizhijia.com/wenda/?question/view/46827.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
